//
//  GYProductCollectionView.m
//  GYDemo
//
//  Created by Mrzhang on 2018/3/26.
//  Copyright © 2018年 mac. All rights reserved.
//

#define REUSE_ID            @"hms.class.reuse.id"
#define REUSEVIEW_ID        @"hms.class.reuseview.id"
#define REUSEFOOTERVIEW_ID  @"hms.class.reusefooterview.id"

#import "GYProductCollectionView.h"
#import "Head.h"

@interface GYProductCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *mCollection;

@end
@implementation GYProductCollectionView 
@synthesize parentid,mCollection,dataSource,brandSource;

-(void)refreshData:(GYProductCateModel *)model
{
    dataSource=[[NSMutableArray alloc] initWithArray:model.secLevelArr];
    [mCollection reloadData];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        CGFloat size = WFCGFloatX((375 -140) / 2);
        
        [flowLayout setItemSize:CGSizeMake(size, size * 0.8 + WFCGFloatY(18 + 8))];//设置cell的尺寸
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];//设置其布局方向
        flowLayout.sectionInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);//设置其边界
        flowLayout.minimumInteritemSpacing = 0.0f;
        flowLayout.minimumLineSpacing = 10;
        //其布局很有意思，当你的cell设置大小后，一行多少个cell，由cell的宽度决定
        
        mCollection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        
        mCollection.dataSource = self;
        mCollection.delegate = self;
        [mCollection setBackgroundColor:[UIColor clearColor]];
        mCollection.showsVerticalScrollIndicator=NO;
        mCollection.showsHorizontalScrollIndicator=NO;
        //注册Cell，必须要有
        [mCollection registerClass:[ClassCollectionCell class] forCellWithReuseIdentifier:REUSE_ID];
        [mCollection registerClass:[ClassReuseView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:REUSEVIEW_ID];

        [self addSubview:mCollection];
    }
    return self;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
 
    NSArray* arr=[dataSource[section] objectForKey:@"list"];
    return arr.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [dataSource count];
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:REUSE_ID forIndexPath:indexPath];
    cell.backgroundColor=[UIColor whiteColor];

    NSArray* arr=[dataSource[indexPath.section] objectForKey:@"list"];
    cell.info=[arr[indexPath.row] objectForKey:@"productType"];
    [cell layoutSubviews];
    return cell;
}

//每个section
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        ClassReuseView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:REUSEVIEW_ID forIndexPath:indexPath];

        NSDictionary* dic=[dataSource[indexPath.section] objectForKey:@"productType"];
        [reusableview.titleLB setText:[dic objectForKey:@"name"]];

        [reusableview.btn setTag:indexPath.section];
        return reusableview;
    }

    return nil;
}
#pragma mark --UICollectionViewDelegateFlowLayout
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(WF_UI_VIEW_WIDTH, WFCGFloatY(43));
}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray* arr=[dataSource[indexPath.section] objectForKey:@"list"];
    NSDictionary* info=[arr[indexPath.row] objectForKey:@"productType"];
    
    _cellBack([info objectForKey:@"id"]);
}
@end


@implementation ClassReuseView
@synthesize titleLB,btn;
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        UIView *point = [[UIView alloc]initWithFrame:CGRectMake(7, frame.size.height / 2 - 3, 6, 6)];
        [point setBackgroundColor:[UIColor lightGrayColor]];
        [WFFunctions WFUIaddbordertoView:point radius:3.0f width:0.001f color:[UIColor lightGrayColor]];
        [point setHidden:YES];
        [self addSubview:point];
        
        titleLB = [[UILabel alloc]initWithFrame:CGRectMake(7, 0, frame.size.width - 38, frame.size.height)];
        [titleLB setFont:[UIFont systemFontOfSize:13 withScale:autoSizeScaleY]];
        [titleLB setTextColor:Main_Color_Black];
        [self addSubview:titleLB];
        
        btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:btn];
    }
    return self;
}
@end



@implementation ClassCollectionCell
@synthesize imageIV,nameLB,info;
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //        [WFFunctions WFUIaddbordertoView:self radius:0 width:0.7f color:HMS_COLOR_CELLSTOKE];
        
        imageIV = [[UIImageView alloc]init];
        [self addSubview:imageIV];
        
        nameLB = [[UILabel alloc]init];
        [nameLB setTextColor:Main_Color_Black];
        [nameLB setFont:[UIFont systemFontOfSize:11 withScale:autoSizeScaleY]];
        [nameLB setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:nameLB];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];

    NSString *imgurl =[WFFunctions WFStrCheckEmpty:[info objectForKey:@"imgUrl"]]?@"":[NSString stringWithFormat:@"%@%@",[info objectForKey:@"imgUrl"],kProductImgThumbnailSuffix];
    NSString *name = [WFFunctions WFStrCheckEmpty:[info objectForKey:@"name"]]?@"":[info objectForKey:@"name"];
    [imageIV setFrame:CGRectMake(0, 0, self.width * 0.7, self.width * 0.7)];
    [imageIV setCenter:CGPointMake(self.width / 2, imageIV.height / 2 + WFCGFloatY(8))];
    
    [nameLB setFrame:CGRectMake(0, self.height - WFCGFloatX(26), self.width, WFCGFloatY(26))];
    
    [imageIV sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:DefaultImage];
    [nameLB setText:name];
}
@end

